/* $Header: zone_status.h,v 2.2 01/10/02 10:30:48 przybyls Exp $ */

/***********************************************************************/
/* INCLUDE FILE NAME:  zone_status.h                                   */
/* ==================                                                  */
/*                                                                     */
/* Author:  Lesley Freed                                               */
/*                                                                     */
/* COMMENT:  This file contains the data structure types used in       */
/*           the instrument status data store.                         */
/*                                                                     */
/*                                                                     */
/* Revisions: 9/22/95  added fields for aux?_rdg_count to allow for    */
/*                     variable execution of aux functions in zone_task*/
/*                                                                     */
/*                                                                     */
/*                                                                     */
/***********************************************************************/

#ifndef ZSTAT_H
#define ZSTAT_H


/*****************************/
/*   IFDEFS                  */
/*****************************/
/*-----begin change by wangwei-----------*/
#include "wkfile.h"
#include "zone_config.h"
#include "ramps.h"
/*-----end change------------------------*/

/*****************************/
/*   SYMBOLIC CONSTANTS      */
/*****************************/


/*****************************/
/*   MACROS                  */
/*****************************/


/*****************************/
/*   TYPEDEFS                */
/*****************************/

typedef U_INT32 TEMP_ACTUAL;

typedef U_INT32 TEMP_SETPT;

typedef U_INT32 FILT_SETPT;

typedef struct
{
  U_INT16 *ptable;            /* pointer to the boxcar array */
  U_INT16 residual;           /* remainder from the filter   */
  U_INT16 index;              /* index into the filter array */

} FILT_16U, *pFILT_16U;

typedef enum
{
   CONST_CRYO_DC_MODE     = 0,
   BANG_BANG              = 1,
   FORCED_AIR_ON          = 2,
   FORCED_AIR_OFF         = 3,
   CRYO_CONTROL_MODE      = 4,
   CRYO_BLAST_MODE        = 5,
   CRYO_BLAST_DWELL_MODE  = 6,
   NO_CRYO_CONTROL_MODE   = 7,
   FLAP_COOLING_MODE      = 8,
   SHUTDOWN_BY_OVEN_DOOR  = 9,
   NO_CRYO_INSTALLED_MODE = 10

}  COOLING_MODE;

/* WARNING: Do not change the order of the ZONE_THERMAL_FAULTS- they */
/*          are used as indices into a lookup table in ThermalFault. */
typedef enum
{
  ZONE_TOO_HOT      = 0,
  ZONE_TOO_COOL     = 1,
  ZONE_SENSOR_SHORT = 2

} ZONE_THERMAL_FAULT;

typedef enum
{
  NO_CRYO_SHUTDOWN      = 0,
  CRYO_TIMEOUT_SHUTDOWN = 1,
  CRYO_FAULT_SHUTDOWN   = 2

} CRYO_SHUTDOWN;


typedef enum
{
   OVEN_NOT_PRESENT,
   OVEN_DOOR_NOT_SHUT,
   OVEN_FILT_INIT_IN_PROGRESS,
   OVEN_SAFETY_ON_DELAY,
   OVEN_FAULT,
   OVEN_H2_LEAK_FAULT,
   OVEN_FLAP_INIT_IN_PROGRESS,
   OVEN_OFF,
   OVEN_CONTROL_IN_PROGRESS

} OVEN_STATE;

typedef enum
{
   NORMAL_READY     = 0,
   NEW_FINAL_READY  = 1,
   NEW_RAMP_READY   = 2,
   NEW_STEP_READY   = 3,
   NEW_TEMP_READY   = 4,
   IGNORE_NOT_READY = 5

} ZONE_READY;

typedef struct
{
  unsigned shutdown_by_flow : 1;       /* column flow shutdown, source and   */
                                       /*    outlet also shutdown            */
  unsigned shutdown_by_cryo : 1;       /* cryo shutdown                      */
  unsigned full_on_shutdown : 1;       /* heater full on time > MAX_FULL_ON  */
  unsigned timetable_off    : 1;       /* timetable setpt to turn zone off   */
  unsigned filler_bits      : 13;

} ZONE_SHUTDOWNS;

typedef union
{
   ZONE_SHUTDOWNS flags;
   BIT16          word;

} ZONE_SHUTDOWN;


typedef struct
{
   U_INT16      cryo_ready_count;      /* counter for readiness outside a run */
   U_INT16      cryo_timeout_time;     /* working copy of cryo timeout setpt  */
   U_INT16      cryo_not_ready_count;  /* counter for not readiness in a run  */
   INT32        cryo_accum;            /* cryo integral error                 */
   U_INT32      cryo_residue;          /* residue from cryo duty cycle calc.  */
   U_INT16      cryo_demand;           /* demand for the oven cryo valve      */
   U_INT16      cryo_delay;            /* time period between calculating     */
                                       /* cryo duty cycle                     */
   U_INT16      cryo_on_count;         /* running counter for duty cycle for  */
                                       /* the cryo valve                      */
   U_INT16      period_on_count;       /* on count for immediate cryo period  */
   U_INT16      const_on_count;        /* constant on count for the cryo      */
   U_INT8       const_duty_cycle;      /* % on for valve in const dc mode     */
   ON_OFF       const_duty_cycle_on_off; /* enable/disable const duty cycle   */

   COOLING_MODE cooling_mode;          /* cooling control mode                */

} CRYO_STATUS, *pCRYO_STATUS;

typedef struct
{

  U_INT16       zone_rdg;               /* filtered zone reading             */
  TEMP_ACTUAL   zone_temp;              /* temp in 64*256 ths deg C          */
  TEMP_SETPT    temp_setpt;             /* current temp setpt in 64ths deg C */
  FILT_SETPT    filt_temp_setpt;        /* filtered setpt in 256*64ths deg C */

  U_INT8        low_freq_filt_size;     /* size of the low freq filter boxcar */

  FILT_16U      low_freq_filt;             /* low freq boxcar filter data     */
  U_INT16       filter_table[BOXCAR_4_PT_SIZE ]; /* low freq boxcar table     */

  U_INT16       zone_temp_filter_table[4];
  U_INT32       zone_full_on_timer;     /* zone full on timer, duty cycle=100%*/
  U_INT32       zone_rdy_timer;         /* zone readiness count up timer      */
  U_INT16       ignore_not_rdy_timer;   /* ignore not ready count down timer  */
  U_INT16       cryo_blast_timer;       /* down timer: time at setpt in blast */
  ZONE_READY    ready_eval_state;       /* state for readiness evaluation     */
  RAMP_FLAG     prev_ramp_state;        /* previous ramp program state        */
  INT32         zone_integ_accum;
  U_INT8        zone_integ_delay;
  U_INT8        zone_temp_err;          /* counter for zone errors            */
  INT32         zone_dev;               /* current (unfiltered) deviation     */
  INT32         zone_old_dev_avg;       /* filtered deviation of reading n-1  */
  INT32         zone_new_dev_avg;       /* filtered deviation of reading n    */
  U_INT8        filt_setpt_residual;    /* fractional part of filtered setpt  */
  INT16         zone_demand;
  INT16         zone_residue;
  INT16         raw_demand;             /* raw demand value used by zone for  */
                                        /* cryo and flap control              */
  U_INT8        zone_not_installed_count; /* counter for # of open rtd rdgs   */
  BIT8          on_mask;                /* on mask for zone on/off status     */
  U_INT16       wattage;                /* wattage of the zone                */

  ZONE_SHUTDOWN shutdowns;              /* bit field of shutdowns             */

  U_INT8        const_duty_cycle;        /* constant duty cycle drive         */
  ON_OFF        const_duty_cycle_on_off; /* enable/disable const duty cycle   */

  RAMPINFO      *ramp_info;             /* pointer to ramp state information  */
  ZONE_CONFIG   *config;                /* pointer to configuration values    */
  CRYO_SETPTS   *cryo;                  /* pointer to the cryo setpts         */
  CRYO_STATUS   *cryo_status;           /* pointer to cryo working variables  */
  ZONE_TYPE     slave;                  /* location of "slave" zone           */
                                        /* (FPD det temp uses this)           */
  ZONE_TYPE     master;                 /* location of "master" zone          */
                                        /* (FPD aux temp uses this)           */

} ZONE_TABLE, *pZONE_TABLE;

typedef struct
{
   INT16 value;
   INT16 filt_value;
   INT16 residual;

} IIR_FILT_INFO, *pIIR_FILT_INFO;

typedef struct
{
   INT16   noise;
   INT16   offset;
   U_INT16 Y1;
   U_INT16 Y2;
   INT32   delta_Y;
   INT32   calc_offset;
   U_INT8  err_count;

} MUX_ADC_INFO;

typedef struct
{
        unsigned oven_shutdown_by_flow : 1;
        unsigned flap_init_request     : 1;
        unsigned oven_hold_off_active  : 1;
        unsigned oven_on_init_request  : 1;
        unsigned oven_door_open        : 1;
        unsigned new_oven_max_temp     : 1;
        unsigned inja_filter_reset     : 1;
        unsigned injb_filter_reset     : 1;
        unsigned auxa_filter_reset     : 1;
        unsigned auxb_filter_reset     : 1;
        unsigned deta_filter_reset     : 1;
        unsigned detb_filter_reset     : 1;
        unsigned filler_bits           : 4;

} THERMAL_FLAGS;

typedef union
{
        THERMAL_FLAGS flags;
        BIT16         word;

} THERMAL;

typedef struct
{
/* unused bits */
        unsigned filler_bits           : 8;

/* Non-fatal errors: these are pretty serious */
        unsigned thermal_fault         : 1;
        unsigned hydrogen_shutdown     : 1;

/* shutdowns: these are causing the oven to be off, required setpt change to */
/* on, off or temp change to clear.                                          */
        unsigned flow_shutdown         : 1;
        unsigned cryo_scram_active     : 1;
        unsigned oven_scram_active     : 1;

/* power-fail recovery and method load: cause the oven to be off until the */
/* core instrument is ready (flows, temps, and det control)                */
        unsigned oven_hold_off         : 1;

/* door is open, will automatically turn on when it is closed (if setpt is on)*/
        unsigned oven_door_open        : 1;

/* status: oven is operating at full power */
        unsigned oven_at_max_power     : 1;

} OVEN_FLAGS;

typedef union
{
        OVEN_FLAGS  flags;
        BIT16       word;

} OVEN_STATUS;

typedef struct
{
        BIT8    zones_on_off;           /*  zones ON/OFF flags (1=ON)        */
        BIT8    zones_at_max_power;     /*  zone at full power (1=TRUE)      */
        BIT8    zone_hot_fault;         /*  Zone Too Hot Flags (1=TOO HOT)   */
        BIT8    zone_cool_fault;        /*  Zone Too Cool Flags (1=TOO COOL) */
        BIT8    zone_sensor_short;      /*  Zone Reading less than minimum   */
                                        /*  possible (1=SENSOR SHORT)        */
        BIT8    zones_in_control;       /*  Zone in Control Flags            */
                                        /*  (1=IN CONTROL)                   */
        BIT8    zones_init;             /*  countdown counter to init filters*/
        BIT8    cryo_active;            /*  bit fields for cryo active       */
        BIT8    cryo_blast_allow;       /*  bit fields to enable cryo blast  */
        U_INT8  zone_task_index;        /*  index into zone task table       */
        U_INT8  zone_task_tick;         /*  tick count for which zone(s) to  */
                                        /*  process in the zone task         */
        U_INT8  line_power;             /*  calculated line power            */
        U_INT8  line_sense_err_count;   /*  count of # of bad line sense rdgs*/
        U_INT16 wattage70;              /*  calculated wattage of 70W heater */
        U_INT16 wattage150;             /*  calculated wattage of 150W htr   */
        THERMAL state;                  /*  flag bits for thermal sys state  */
        U_INT16 oven_estimate;          /*  estimated oven temperature       */
        INT16   raw_oven_demand;        /*  unclamped/unscaled oven demand   */
        U_INT8  oven_scram_error_count; /*  count for oven monitor errors    */
        U_INT8  oven_on_init_count;     /*  count for oven on initialization */
        U_INT8  inja_rdg_count;         /*  down-counter for # of rdgs       */
        U_INT8  no_of_inja_rdgs;        /*  # of front inlet readings to     */
                                        /*  count down by to get control rate*/
        U_INT8  injb_rdg_count;         /*  down-counter for # of rdgs       */
        U_INT8  no_of_injb_rdgs;        /*  # of back  inlet readings to     */
                                        /*  count down by to get control rate*/
        U_INT8  auxa_rdg_count;         /*  down-counter for # of rdgs       */
        U_INT8  no_of_auxa_rdgs;        /*  # of front aux   readings to     */
                                        /*  count down by to get control rate*/
        U_INT8  auxb_rdg_count;         /*  down-counter for # of rdgs       */
        U_INT8  no_of_auxb_rdgs;        /*  # of back  aux   readings to     */
                                        /*  count down by to get control rate*/
        U_INT8  deta_rdg_count;         /*  down-counter for # of rdgs       */
        U_INT8  detb_rdg_count;         /*  down-counter for # of rdgs       */
        U_INT8  line_voltage_percent;   /*  line voltage, nominal = 100      */
        U_INT16 secondary_voltage;      /*  zone supply voltage in 10mV units*/

        BIT8    cryo_valves_used;       /*  flag bits for sharing cryo valve */
                                        /*  output with the manual ss inlet  */
} ZONE_STATUS;


typedef struct
{
        U_INT16         flap_setpt;     /* flap position setpt               */
        U_INT16         flap_actual;    /* actual flap position              */
        INT32           flap_accum;     /* accumulated flap position error   */
        U_INT8          flap_init;      /* countdown counter to init flap    */
        U_INT8          flap_tick_count;/* counter for 10.4 Hz LED driver    */
        BIT8            flap_toggle;    /* toggle to drive flap when closed  */

} FLAP_STATUS;

/*****************************/
/*   EXTERNAL VARIABLES      */
/*****************************/

/* Thermal status for Oven and Zones */

#pragma SECTION DATA=data

extern  U_INT16         Zone_Msg_No;
extern  U_INT16         Zone_Type_Msg;
extern  ZONE_TYPE       Zone_Tune_Type;

extern  ZONE_STATUS     Zone_Status;
extern  FLAP_STATUS     Flap_Status;

extern  CRYO_STATUS     Oven_Cryo_Status;
extern  CRYO_STATUS     Inja_Cryo_Status;
extern  CRYO_STATUS     Injb_Cryo_Status;
extern  CRYO_STATUS     Auxa_Cryo_Status;
extern  CRYO_STATUS     Auxb_Cryo_Status;


extern  ZONE_TABLE      *pZone_Table;       /* ptr to zone being processed */

extern  ZONE_TABLE      Oven_Table;
extern  ZONE_TABLE      Deta_Table;
extern  ZONE_TABLE      Detb_Table;
extern  ZONE_TABLE      Inja_Table;
extern  ZONE_TABLE      Injb_Table;
extern  ZONE_TABLE      Auxa_Table;
extern  ZONE_TABLE      Auxb_Table;

/* Mux adc status */

extern  BIT32           Mux_Adc_Rdgs;     /* Flags for new mux'ed adc rdgs */
extern  MUX_ADC_INFO    Mux_Adc_Offset;   /* Signed mux adc offset         */

/* The following variables are used for thermal zone tuning */

extern  ZONE_TABLE      *pZone_Tune;            /* ptr to zone being tuned */
extern  IIR_FILT_INFO   Demand;
extern  IIR_FILT_INFO   Raw_Demand;
extern  INT32           Prop_Demand;
extern  INT32           Deriv_Demand;
extern  INT32           Integ_Demand;
extern  U_INT8          Integ_Delay;
extern  INT32           Dev;

#pragma SECTION UNDO

#endif
